using System;
using System.Collections;
using DailyReport.Core.Bussines.IManagers;
using DailyReport.Core.Data.Dao;
using DailyReport.Core.Data.Dao.DaoFactory;
using DailyReport.Core.Data.Model;

namespace DailyReport.Core.Bussines
{
    public class UserManager : IUserManager<User>
    {
        public UserManager()
        {
        }

        public void createUser(int teamId, string firstName, string lastName)
        {
            User u = new User();
            u.FirstName = firstName;
            u.LastName = lastName;
            if (teamId > 0)
            {
                ITeamDao teamDao = (ITeamDao)DaoFactory.i.getDaoInstance("TeamDao");
                u.Team = teamDao.GetById(teamId, false);
            }
            IUserDao userDao = (IUserDao)DaoFactory.i.getDaoInstance("UserDao");
            userDao.Save(u);
        }

        public void updateUser(int userId, int teamId, string firstName, string lastName)
        {
            IUserDao userDao = (IUserDao)DaoFactory.i.getDaoInstance("UserDao");
            User u = userDao.GetById(userId, false);
            u.FirstName = firstName;
            u.LastName = lastName;

            if (teamId > 0)
            {
                ITeamDao teamDao = (ITeamDao)DaoFactory.i.getDaoInstance("TeamDao");
                u.Team = teamDao.GetById(teamId, false);
            }
            userDao.SaveOrUpdate(u);
        }

        public void deleteUser(int userId)
        {
            IUserDao userDao = (IUserDao)DaoFactory.i.getDaoInstance("UserDao");
            User u = userDao.GetById(userId, false);
            userDao.Delete(u);
        }

        public User loadUser(int userId)
        {
            IUserDao userDao = (IUserDao)DaoFactory.i.getDaoInstance("UserDao");
            User u = userDao.GetById(userId, true);
            return (u);
        }

        public IList GetInstanceList(string sortExpression, int startRowIndex, int maxRows)
        {
            IUserDao userDao = (IUserDao)DaoFactory.i.getDaoInstance("UserDao");
            return (userDao.getInstancesList(sortExpression, startRowIndex, maxRows));
        }

        public int CountInstances()
        {
            IUserDao userDao = (IUserDao)DaoFactory.i.getDaoInstance("UserDao");
            return (userDao.getInstancesCount());
        }
    }
}
